(function($){
    $.util = {
        len:function(str){ //判断字符长度
            return str.replace(/[^\x00-\xff]/g,"**").length;
        },
        checkedval:function(ele){
            var ret = [];
            ele.each(function(){
                ret.push(this.value);
            });
            return ret;
        },
        show:function(){
            $(".state_tip").html("<img src=\"template/admin/images/loading2.gif\" border=\"0\" align=\"absmiddle\"/>正在执行").fadeIn();
        },
        hide:function(){
            $(".state_tip").html("操作完成").fadeOut('slow');
        }
    }
})(jQuery);
//类目管理，类目排序
(function($){
    $.oper = {
        
        url:'', //操作url
        init:function(url) 
        { //初始化 
            this.url = url;
            return this;
        },
        edit:function(obj,id)
        { //ajax编辑
            var $this = $(obj);
            
            if($this.find("input").length == 0){
                var txt      = $this.text(),
                    size     = parseInt(txt.length),
                    $span    = $("<input type = 'text' value='"+txt+"' size='"+ (size>70?70:size) +"'/>");
                    setValue = function()
                    { //向服务器提交修改后的数据
                            var spanval = $span.val();
                            if(spanval && spanval != txt)
                            {
                                $.util.show();
                                var postdata = {value:spanval,id:id};
                                $.ajax({
                                    type:'POST',
                                    url:"orderType",
                                    data:{"sort":spanval,"typeid":id},
                                    dataType:'text',
                                    success: function(message)
                                    {    
                                        //$.util.hide();

                                        if(message == "success")
                                        { //数据更新成功
                                            $this.text(spanval);
                                            
                                        }else
                                        { //数据更新失败
                                            $this.text(txt);
                                            alert("发生错误,请刷新页面后重新执行该操作","警告");
                                        }
                                        
                                    }
                                });

                            } 
                            else 
                            {
                                $this.text(txt);
                            }
                    }
                $this.html($span);
                $span.focus().select();
                $span.keyup(
                    function(event)
                    { 
                        if(event.keyCode == "13")
                        {
                            setValue();

                        }
                }).blur(function(){
                    setValue();
                    //location.reload();
                });
            }
            else {
                return ;
            }
        },

        bgcolor:function(obj,type) {
            $(obj).css('backgroundColor',type=='on' ? '#0b819f':'');
        },

    }
})(jQuery);

(function($)
{
    $.pageSize={
        setSize:function(e){

            //var $this=$(e);
            var size=$('#pagesize').val();
        //  var size=$this.val();
            //将页数保存到cookie中
            var date = new Date();
            date.setTime(date.getTime() + (5* 60 * 1000));
            $.cookie('pagesize', size, { expires: date }); 
            $.Ajaxsearch();
        }
    }
})(jQuery);

(function($){
    var ajaxsearch=function(page)
    {
        //获取搜索条件
        var page = arguments[0]?arguments[0] : 1;
        var $ordersn=$("input[name='ordersn']").val();

        var $starttime=$("#start").val();

        var $endtime=$("#end").val();    //$("select option:selected").val();
        var $areatype=$("select[name='areatype'] option:selected").val();
        var $paytype=$("select[name='paytype'] option:selected").val();
        var $orderstatus=$("select[name='orderstatus'] option:selected").val();
        var $shopid=$("select[name='shopid'] option:selected").val();
        $.ajax(
        {
            type:'POST',
            url:"searchOrder",
            data:{"ordersn":$ordersn,'starttime':$starttime,'paytype':$paytype,"areatype":$areatype,"endtime":$endtime,"orderstatus":$orderstatus,"page":page,"shopid":$shopid},
            dateType:'JSON',
            success:function(result)
            {   
                var result=jQuery.parseJSON(result);
                var $page=result.pagearr; //底部分页
                var $orders=result.orders;//订单
                var $tbody=$('<tbody></tbody>');
                var $string='';
          if (result.status)
                {
                    
                    $.each($orders,function(key,order){
                        //console.log(value.companyname+key);
                        $string+="<tr>";
                        $string+='<td><input type="checkbox" name="subBox" value='+order.orderid +"></td>";
                        $string+="<td>"+order.orderid +"</td>";
                        $string+="<td>"+order.order_sn+"</td>";
                        $string+="<td>"+order.number +"</td>";
                        $string+="<td>"+order.price +"</td>";
                        $string+="<td>"+order.supperprice +"</td>";
            
                        $string+="<td>"+order.pay_type +"</td>"; 
                        $string+="<td>"+order.companyname +"</td>";
                        $string+="<td>"+order.create_time +"</td>";
                        $string+="<td>"+order.allstatus +"</td>";
                        $string+="<td>"+order.area +"</td>";
                        $string+="<td><span class='am-icon-pencil-square-o'></span><a href=editOrder/orderid/"+order.orderid+">详情</td>";
                        $string+="<td> <span class='am-icon-trash-o'></span><a href='javascript:void(0)' class='delorder' value="+order.orderid+">删除</td>";
                        $string+="</tr>";   
                    });
                    $tbody.append($string);
                    $($tbody).replaceAll(".ordertable tbody");

                    //替换分页
                    $(".count").html($page.count);
                    $(".currentpage").html($page.page);
                    //$("#pagesize").val($page.pagesize);
                    $('.prepage').attr('value',$page.prevpage);
                    $('.nexpage').attr('value', $page.nextpage);
                    $('.laspage').attr('value',$page.pagetotal);
                }else
                {
                     $tbody.append(result.message);
                    $($tbody).replaceAll('.ordertable tbody');
                }
            }

        });
    }

 $.Ajaxsearch=ajaxsearch;

})(jQuery);

//翻页事件
(function($){
     $.pageajax=function(name)
    {
         var pagevalue=$('.'+name).attr('value');
         $.Ajaxsearch(pagevalue);
        return false;
    }
})(jQuery);
(function(){
    $('#search').click(function(){
          $.Ajaxsearch();
      event.preventDefault();
        

    });
})(jQuery);
//删除订单
(function($){
    $(document).on('click','.delorder',function(){
        var res=confirm("确定删除订单");
        if(!res){
          return false;
        }
        var id=$(this).attr('value');
        $.ajax({
            type:"GET",
            data:{"orderid":id},
            url: "delOrder",
            dataType:"json",
            success:function(message)
            {
                if (message.status)
                {
                    alert(message.msg);
                    location.reload();
                 }else{
                 alert(message.msg);
                  location.reload();
                }
            }
        })
   })
})(jQuery);
//全选
(function(){
    $(function(){
           $("#checkAll").click(function() {
                $('input[name="subBox"]').prop("checked",this.checked); 
            });
            var $subBox = $("input[name='subBox']");
            $subBox.click(function(){
                $("#checkAll").prop("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
            });
        }); 
})(jQuery);

//导出订单
(function(){
     $(function(){
      $('.putcsv').click(function(){
          //获取所有选中订单
           var orderids="";
            $("input[name='subBox']:checkbox").each(function()
            { 
                if($(this).prop("checked"))
                {
                    orderids += $(this).val()+","

                }
            })
            
         window.open("putCsv?orderids="+orderids);
          
      });
  });
})(jQuery)
//日期插件
;(function($){
    //日期插件

     $.start =
       {
        elem: '#start',
        format: 'YYYY-MM-DD hh:mm:ss',
        min: '1999-06-16 23:59:59', //设定最小日期为当前日期
        max: '2099-06-16 23:59:59', //最大日期
        istime: true,
        istoday: false,
        choose: function(datas)
            {
                 end.min = datas; //开始日选好后，重置结束日的最小日期
                 end.start = datas //将结束日的初始值设定为开始日
            }
        };
     $.end=
         {
            elem: '#end',
            format: 'YYYY-MM-DD hh:mm:ss',
            min: '1999-06-16 23:59:59',
            max: '2099-06-16 23:59:59',
            istime: true,
            istoday: false,
            choose: function(datas)
                {
                    start.max = datas; //结束日选好后，重置开始日的最大日期
                }
         };
//laydate(start);
//laydate(end);
})(jQuery);

//冻结运营商
;
             $(document).on('click','.frozen',function(){
            var yid=$(this).attr('value');
            var id=$(this).attr('id');///获取运营商的状态值
                console.log(id);
            if(id==0)
            {/////未冻结的运营商
                var result =confirm('确定冻结 此运营商');
            if (!result)
            {
                return '';
            };
            ////发送ajax st
              $.ajax({
                type:'POST',
                url:"/admin.php/Home/Yunying/frozen",
                data:{'yid':yid},
                dataType:"json",
                success:function(data)
                {
                    if(data.status == false)
                    {
                        alert(data.msg);
                        return '';

                    }
                            location.reload();
                            
                        }
                    });
              ////发送ajax en
            }else
            {
             //////解冻运营商
               var res =confirm('对该运营商实行解冻操作');
               if(!res)
               {
                  return '';
               };
               ////发送ajax st
              $.ajax({
                type:'POST',
                url:"/admin.php/Home/Yunying/Thaw",
                data:{'yid':yid},
                dataType:"json",
                success:function(data)
                {
                    if(data.status == false)
                    {
                        alert(data.msg);
                        return '';

                    }
                            location.reload();
                            
                        }
                    });
              ////发送ajax en

            }
          
        });

//检测运营商唯一性，根据登录帐号，和公司名称，不能重复
;(function($){
    $('.checkyun').click(function(event){
  
    event.preventDefault();
    var $action=$("input[name='action']").val();
    var $cname=$("input[name='companyname']").val();
    var $yname=$("input[name='yname']").val();
    var $yid=$("input[name='yid']").val();
    switch($action)
    { 
      case 'addyun':
        $.ajax({
          type:'POST',
          url:'weiyi',
          data:{"companyname":$cname,"yname":$yname,"action":$action},
          dataType:'json',
          success:function(message)
          { 
            if(message.status ==false) 
              {
                alert(message.msg);

              }else{
               $("#yun").submit();
              }
          }
        })
      break;
      case 'edityun':
        $.ajax({
          type:'POST',
          url:'/admin.php/Home/Yunying/weiyi',
          data:{"companyname":$cname,"yname":$yname,"action":$action,"yid":$yid},
          dataType:'json',
          success:function(message)
          { 
            if(message.status ==false) 
              {
                alert(message.msg);

              }else{
               $("#yun").submit();
              }
          }
        })
        // $("#yun").submit();
      break;
    }
  
  })
})(jQuery)

//统一检测表单必填字段
;(function($){
    $.reqfild=function(e)
    {
            switch(e)
            {
            case "yun":
                $('#yun').validate({
                rules:{
                        yname:{
                          required:true,
                          minlength:2,
                        },
                        companyname:{
                            required:true,
                            minlength:3,
                        },
                        connectname:{
                            required:true,
                            minlength:2,
                        },
                        area:{
                            required:true,
                            minlength:2,
                        },
                        mobile:{
                            required:true,
                            minlength:2,
                          },
                         address:{
                            required:true,
                            minlength:2,
                        },
                        pass: {
                            required: false,
                            minlength: 3
                        },
                        twopass:{
                            required: false,
                            minlength: 3,
                            equalTo: "#pass"
                        }
                        
                    }
              });
            break;
            

            }
    }
})(jQuery)

//商品管理
;(function($){
    $(document).on('click','.itemstatus',function(){
        var $this=$(this);
        var $itemid=$this.attr('value');
        var  $class=$this.children().attr('class');
        if ($class== 'am-icon-arrow-up') { $action='down'}
        if ($class== 'am-icon-arrow-down') { $action='up'}
        $.ajax({
            type:"POST",
            url:"/admin.php/Home/Item/editStatus",
            data:{"itemid":$itemid,"action":$action},
            dataType:'json',
            success:function(message){
                if (message.status ==false) 
                    { 
                        alert(message.msg);return false;
                    }else{
                        //更改css 状态
                        if (message.action=='up') 
                            {
                                $this.children().attr('class','am-icon-arrow-up');
                            }else if(message.action=='down'){
                                $this.children().attr('class','am-icon-arrow-down');
                            }
                        
                    }
            }
        });
    })
})(jQuery)

//商品管理-商品筛选

;(function($){
    //筛选条件
  //公用ajax 晒需条件
  $.searchitem=function(page,keywords)
  {

     //获取搜索条件
     var page = arguments[0]?arguments[0] : 1;
     var keywords=arguments[1]?arguments[1] :'';
     var $typeid=$("select[name='typeid'] option:selected").val();
     var $brandid=$("select[name='brandid'] option:selected").val();
     var $platid=$("input[name='platid']:checked").val();
      //缓存类目和品牌
     var date = new Date();
     date.setTime(date.getTime() + (5* 60 * 1000));
     $.cookie('typeid', $typeid, { expires: date,path:'/',});
     $.cookie('brandid', $brandid, { expires: date,path:'/' });
     $.cookie('keywords', keywords, { expires: date,path:'/' });
      $.cookie('platid', $platid, { expires: date,path:'/' });
     $.ajax({
        type:'POST',
        url:"/admin.php/Home/Item/getItem",
        data:{"typeid":$typeid,"brandid":$brandid,"page":page,"keywords":keywords,"platid":$platid},
        dataType:"json",
        success:function(message){
         //解析json 数据
           if(message.status){
              var $page=message.pagearr; //底部分页
              var $items=message.item;//订单
              var $tbody=$('<tbody></tbody>');
              var $string='';
              $.each($items,function(key,item){
                  $string+="<tr class=item_"+item.itemid+">";
                  $string+='<td><input type="checkbox"></td>';
                  $string+="<td>"+item.itemid+"</td>";
                  $string+=(item.isspeci !=false)?"<td>"+'<a href="javascript:void(0)" class="bindaddLayer" value="'+item.itemid +'"><span class=" am-icon-plus-square-o"></span></a>'+item.itemname+"</td>":"<td>"+item.itemname+"</td>";
                  $string+="<td>"+item.bn+"</td>";
                  $string+="<td><span class='itemprice' value='price' id="+item.itemid+">"+item.price+"</span></td>";
                  $string+="<td><span class='supprice' value='supprice' id="+item.itemid+">"+item.supperprice+"</span></td>";
                  $string+="<td><span class='inventory' value='inventory' id="+item.itemid+">"+item.inventory+"</span></td>";
                  $string+="<td>"+item.createtime+"</td>";
                  //产品状态
                  if(item.status ==="1")
                  {
                    $string+='<td><a  href="javascript:void(0)" value='+item.itemid+' class="itemstatus"> <span class="am-icon-arrow-up"></span> </a></td>' ;
                  }else{
                     $string+=' <td><a  href="javascript:void(0)" value='+item.itemid+' class="itemstatus"> <span class="am-icon-arrow-down"></span> </a></td>' ;
                  }

                  $string+='<td><div class="am-btn-toolbar"><div class="am-btn-group am-btn-group-xs">';
                  $string+=(item.platid !=false)?'<span class="am-icon-pencil-square-o"></span><a href=/admin.php/Home/Item/editItem?itemid='+item.itemid+'>编辑</a>':'';
                  $string+='<span class=am-icon-trash-o></span><a href="javascript:void(0)" class="delitem" value='+item.itemid+'> 删除 </a>';
                  $string+='<span class=am-icon-eye></span><a href=/admin.php/Home/Item/viewItem?itemid='+item.itemid+'  target="_blank">预览</a>';
                  //审核运营商产品
                  if( item.examine==0){
                        $string+='&nbsp;<span class=am-icon-bell-o></span><a href="javascript:void(0)" class="examineitem" value='+item.itemid+'> 未审核 </a>';
                  }else{
                    $string+='&nbsp;<span class=am-icon-bell-o></span><a href="javascript:void(0)" class="" value='+item.itemid+'> 已审核 </a>';
                  }
                  $string+=(item.yname !=null)?'&nbsp; <a href="javascript:void(0)" title='+item.yname+'><span class=am-icon-info-circle></span></a>':'';
                  $string+="</div></div></td></tr>"; 
              });
              $tbody.append($string);
              $($tbody).replaceAll("tbody");
              //替换分页
              $(".count").html($page.count);
              $(".currentpage").html($page.page);
              $("#pagesize").val($page.pagesize);
              $('.prepage').attr('value',$page.prevpage);
              $('.nexpage').attr('value',$page.nextpage);
              $('.laspage').attr('value',$page.pagetotal);
              $('.pagetotal').html($page.pagetotal);

           }else{
               $('tbody').html('暂无数据');
           }
        }

     });
  }
  //筛选数据
  $("#filter").click(function(){
      $.searchitem();
  })
  //关键字搜索产品
  $('.keyseach').click(function(){
    var keywords=$("input[name='keywords']").val();
     $.searchitem(1,keywords);
     return false;
  });

  /*审核运营商产品*/
 $(document).on('click','.examineitem',function(){
        var itemnode=$(this);       
        var res=confirm("确定审核");
        if(!res){
          return false;
        }
        var id=$(this).attr('value');
        $.ajax( {
            type:"GET",
            data:{"itemid":id},
            url: "/admin.php/Home/Item/examineItem",
            dataType:"json",
            success:function(message){
                if (message.status)
                {   
      
                    itemnode.text('已审核');
                    itemnode.prop('class','');
                    return '';
                 }else{
                 alert(message.msg);
                  
                }
                }


          })
   })

//ajax 动态生成的元素绑定事件
   $(document).on('click','.delitem',function(){
        var res=confirm("确定删除");
        if(!res){
          return false;
        }
        var id=$(this).attr('value');
        $.ajax( {
            type:"GET",
            data:{"itemid":id},
            url: "/admin.php/Home/Item/delItem",
            dataType:"json",
            success:function(message){
                if (message.status)
                {
                    alert(message.msg);
                    var pnode=$('.item_'+id);
                   $('tbody .item_'+id ).remove();
                 }else{
                 alert(message.msg);
                  location.reload();
                }
                }


          })
   })
})(jQuery)
//商品管理-动态修改库存、零售价、供货价
;(function($){
    $.ajaxEdit= {
        
        url:'', //操作url
        init:function(url) 
        { //初始化 
            this.url = url;
            return this;
        },
        edit:function(obj,action,val,id)
        { //ajax编辑
            var $this = obj;
            var $id=$this.attr('id');
            var $action=$this.attr('value');
            if($this.find("input").length == 0){
                var txt      = $this.text(),
                    size     = parseInt(txt.length),
                    $span    = $("<input type = 'text' value='"+txt+"' size='"+ (size>70?70:size) +"'/>");
                    
                    setValue = function()
                    { //向服务器提交修改后的数据
                            var spanval = $span.val();
                            if(spanval && spanval != txt)
                            {
                                
                                $.ajax({
                                    type:'POST',
                                    url:"/admin.php/Home/Item/ajaxEditItem",
                                    data:{"spanval":spanval,"itemid":$id,"action":$action},
                                    dataType:'json',
                                    success: function(message)
                                    {    
                                        //$.util.hide();

                                        if(message.status)
                                        { //数据更新成功
                                            $this.text(spanval);
                                            
                                        }else
                                        { //数据更新失败
                                            $this.text(txt);
                                            alert(message.msg);
                                        }
                                        
                                    }
                                });

                            } 
                            else 
                            {
                                $this.text(txt);
                            }
                    }
                $this.html($span);
                $span.focus().select();
                $span.keyup(
                    function(event)
                    { 
                        if(event.keyCode == "13")
                        {
                            setValue();

                        }
                }).blur(function(){
                    setValue();
                    //location.reload();
                });
            }
            else {
                return ;
            }
        },

        bgcolor:function(obj,type) {
            $(obj).css('backgroundColor',type=='on' ? '#0b819f':'');
        },

    }

    $(document).on('click','.itemprice,.supprice,.inventory',function(){
        var $this=$(this);
        //调用Ajax修改
        $.ajaxEdit.edit($this);
    })

    /*$('.itemprice,.supprice,.inventory').click(function(){
        var $this=$(this);
        //调用Ajax修改
        $.ajaxEdit.edit($this);
    });*/

})(jQuery)
/*
 ×弹出商品规格
 */
;(function($){
      $.ItemLayer=
      {
          addLayer:function(e)
          {
                 var itemid=(e!=false)?e:0; //商品ID 
                 //获取商品的颜色，价格，库存，名称，型号
                 $.ajax
                 (
                        {
                            type:'post',
                            url:"/admin.php/Home/Item/ajaxProduct",
                            data:{'itemid':itemid},
                            dataType:"json",
                            success:function(message)
                            {
                                var $tbody=$('<tbody></tbody>');
                                var $string='';
                                var status=message.status;
                                var $products=message.products;
                                if(status)
                                {
                                        
                                    $.each($products,function(key,product){
                                        $string+='<tr><td>'+product.productid+'</td>';
                                        $string+='<td> <span class="prodcutcolor" value="prodcutcolor" id="'+product.productid+'">'+ product.color+'</span></td>';
                                        $string+='<td> <span class="prodcutprice" value="prodcutprice" id="'+product.productid+'">'+ product.price+'</span></td>';
                                        $string+='<td> <span class="prodcutsupprice" value="prodcutsupprice" id="'+product.productid+'">'+ product.supprice+'</span></td>';
                                        $string+='<td> <span class="productinventory" value="productinventory" id="'+product.productid+'">'+ product.inventory+'</span></td>';
                                        $string+='</tr>';
                                    })
                                  //设置弹出层的itemid 的值
                                  $("input[name='itemidvalue']").val(itemid);
                                  $tbody.append($string);
                                  $($tbody).replaceAll(".protable tbody");

                                }else{
                                     var $tbody=$('<tbody>暂无规格</tbody>');
                                     $($tbody).replaceAll(".protable tbody");
                                }
                            }
                          }
                  );           
          }, //弹出product结束

        //ajax 直接修改规格start
        url:'', //操作url
        init:function(url) 
        { //初始化 
            this.url = url;
            return this;
        },
        edit:function(obj)
        { //ajax编辑
            var $this = obj;
            var $id=$this.attr('id');
            var $action=$this.attr('value');
            //获取商品itemid
            var $itemid= $("input[name='itemidvalue']").val();
            if($this.find("input").length == 0){
                var txt      = $this.text(),
                    size     = parseInt(txt.length),
                    $span    = $("<input type = 'text' value='"+txt+"' size='"+ (size>70?70:size) +"'/>");
                    
                    setValue = function()
                    { //向服务器提交修改后的数据
                            var spanval = $span.val();
                            if(spanval && spanval != txt)
                            {
                                
                                $.ajax({
                                    type:'POST',
                                    url:"/admin.php/Home/Item/ajaxEditproduct",
                                    data:{"spanval":spanval,"productid":$id,"action":$action,'itemid':$itemid},
                                    dataType:'json',
                                    success: function(message)
                                    {    
                                        //$.util.hide();

                                        if(message.status)
                                        { //数据更新成功
                                            $this.text(spanval);
                                            //更新基础库存
                                            var itemid=message.itemid; //商品ID
                                            var iteminven= message.suminven;//库存
                                            $('.item_'+itemid+' .inventory').html(iteminven);
                                            
                                        }else{ //数据更新失败
                                            $this.text(txt);
                                            alert(message.msg);
                                        }
                                        
                                    }
                                });

                            } 
                            else 
                            {
                                $this.text(txt);
                            }
                    }
                $this.html($span);
                $span.focus().select();
                $span.keyup(
                    function(event)
                    { 
                        if(event.keyCode == "13")
                        {
                            setValue();

                        }
                }).blur(function(){
                    setValue();
                    //location.reload();
                });
            }
            else {
                return ;
            }
        },

        bgcolor:function(obj,type) {
            $(obj).css('backgroundColor',type=='on' ? '#0b819f':'');
        },
          //ajax 直接修改规格end

      };
      /* 为动态添加product 添加事件*/
      $(document).on('click','.prodcutcolor,.prodcutprice,.prodcutsupprice,.productinventory',function(){
        var $this=$(this);
        //调用Ajax修改
        
        $.ItemLayer.edit($this);
    })
      //为商品绑定弹出层事件
      $(document).on('click','.bindaddLayer',function(){
        var $this=$(this);
        var itemid=$this.attr('value');
        
        $('#pro_spec').modal();
        
        
        $.ItemLayer.addLayer(itemid);
      });

  
})(jQuery)
/*
 *品牌模块
 */
;(function($){
    $.Brand=
    {
        //search 品牌开始
        search:function()
        {
            var keywords=$("input[name='keywords']").val();
             var   keywords=(keywords== false) ?'':keywords;
             $.ajax({
                        type:'post',
                        url:"/admin.php/Home/Brand/searBrand",
                        data:{'keywords':keywords},
                        dataType:"json",
                        success:function(result)
                        {
                            var statu =result.status;
                            var brands=result.brands;

                            if(statu)
                            {
                                var $tbody=$('<tbody></tbody>');
                                var $string='';
                                $.each(brands,function(key,brand){
                                    $string+='<tr><td><input type="checkbox"></td>';
                                    $string+='<td>'+brand.brandid+'</td>';
                                    $string+='<td><a href="#">'+brand.brandname+'</a></td>';
                                    $string+='<td>'+brand.createtime +'</td>';
                                    $string+='<td><div class=""><div class="am-btn-group am-btn-group-xs"><button class="am-btn am-btn-default am-btn-xs am-text-secondary"><span class="am-icon-pencil-square-o"><a href="/admin.php/Home/Brand/editBrand/brandid/'+brand.brandid+'">编辑</a></span> </button>';
                                    $string+='<button class="am-btn am-btn-default am-btn-xs am-text-danger"><span class="am-icon-trash-o"> <a href="javascript:void(0)" onclick="ajaxdel('+brand.brandid+')"> 删除 </a> </span> </button></div></div></td>';
                                })
                               $tbody.append($string);
                               $($tbody).replaceAll(".brandtalbe tbody");
                               $('.brandpage').hide();

                            }else{
                                
                                $('.brandtalbe tbody').html('暂无数据');
                                 $('.brandpage').hide();
                            }
                        }
                      }
                      );    
        
        },
        //search 品牌结束

    };

    $('#searbrand').click(function(){$.Brand.search()});


})(jQuery)

/*
 *运营商管理模块
 */
;(function(){
    $.Yun=
    {
            //search 运营商开始
        search:function()
        {
            var keywords=$("input[name='keywords']").val();
            var   keywords=(keywords== false) ?'':keywords;

             $.ajax({
                        type:'post',
                        url:"/admin.php/Home/Yunying/searYun",
                        data:{'keywords':keywords},
                        dataType:"json",
                        success:function(result)
                        {
                            var statu =result.status;
                            var users=result.users;

                            if(statu)
                            {
                                var $tbody=$('<tbody></tbody>');
                                var $string='';
                                $.each(users,function(key,user){
                                    $string+='<tr><td><input type="checkbox"></td>';
                                    $string+='<td>'+user.yid+'</td>';
                                    $string+='<td>'+user.connectname+'</td>';
                                    $string+='<td>'+user.mobile +'</td>';
                                    $string+='<td>'+user.companyname+'</td>';
                                    $string+='<td>'+user.area+'</td>';
                                    $string+='<td>'+user.address+'</td>';
                                    $string+='<td>'+user.qq+'</td>';
                                    $string+='<td><div class="am-btn-toolbar"><div class="am-btn-group am-btn-group-xs"><span class="am-icon-pencil-square-o"><a href="/admin.php/Home/Yunying/edit/id/'+user.yid+'">编辑</a></span>&nbsp;';
                                    $string+='<span class="am-icon-pencil-square-o"><a href="javascript:void(0)" value="'+user.yid+'"  id="'+user.isdel+'" class="frozen">'+user.frozen+'</a></span>&nbsp;';
                                    $string+='<span class="am-icon-pencil-square-o"><a href="/admin.php/Home/Yunying/minute/id/'+user.yid+'">详细</a></span>&nbsp;';
                                    $string+='</div></div></td>';
                                })
                               $tbody.append($string);
                               $($tbody).replaceAll(".yuntalbe tbody");
                               $('.yunpage').hide();

                            }else{
                                
                                $('.yuntalbe tbody').html('暂无数据');
                                 $('.yunpage').hide();
                            }
                        }
                      }
                      );    
        
        }, //搜索运营商END
    }
    $('.searYun').click(function(){
        $.Yun.search();
    });
})(jQuery)
/*
 *商家管理模块
 */
;(function(){
    $.Shop=
    {
            //search 商家开始
        search:function()
        {
            var keywords=$("input[name='keywords']").val();
            var   keywords=(keywords== false) ?'':keywords;
             $.ajax({
                        type:'post',
                        url:"/admin.php/Home/Shop/searShop",
                        data:{'keywords':keywords},
                        dataType:"json",
                        success:function(result)
                        {
                            var statu =result.status;
                            var users=result.users;

                            if(statu)
                            {
                                
                                var $tbody=$('<tbody></tbody>');
                                var $string='';
                                $.each(users,function(key,user){
                                    $string+='<tr><td><input type="checkbox"></td>';
                                    $string+='<td>'+user.shopid+'</td>';
                                    $string+='<td>'+user.companyname+'</td>';
                                    $string+='<td>'+user.connectname +'</td>';
                                    $string+='<td>'+user.mobile+'</td>';
                                    $string+='<td>'+user.isfen+'</td>';
                                    $string+='<td>'+user.address+'</td>';
                                    $string+='<td>'+user.create+'</td>';
                                    $string+='<td>'+user.createtime+'</td>';
                                    $string+='<td>'+user.money+'</td>';
                                    $string+='<td><div class=""><div class="am-btn-group am-btn-group-xs"><button class="am-btn am-btn-default am-btn-xs am-text-secondary"><span class="am-icon-pencil-square-o"><a href="/admin.php/Home/Shop/editShop/shopid/'+user.shopid+'">编辑</a></span></button>';
                                    /*$string+='<button class="am-btn am-btn-default am-btn-xs am-text-secondary"><span class="am-icon-pencil-square-o"><a href="/admin.php/Home/Shop/editShop/shopid/'+user.shopid+'">订单</a> </span></button>';
                                    $string+='<button class="am-btn am-btn-default am-btn-xs am-text-secondary"><span class=" am-icon-database"><a href="/admin.php/Home/Shop/getShop/shopid/'+user.shopid+'">账目明细</a> </span></button>';
                                    $string+='<button class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only"><span class="am-icon-trash-o"></span>冻结</button>';*/
                                    $string+=' </div></div></td>';
                                })
                               $tbody.append($string);
                               $($tbody).replaceAll(".shoptable tbody");
                               $('.shoppage').hide();

                            }else{
                                
                                $('.shoptable tbody').html('暂无数据');
                                 $('.shopage').hide();
                            }
                        }
                      }
                      );    
        
        }, //搜索商家END
    }
    $('.searShop').click(function(){
        $.Shop.search();
    });
})(jQuery)
/*
 *活动产品模块
 */
;(function($){
    $.Activi_Product=
    {
        searchitem:function()
        {
            var page=arguments[0];  //请求页数 
            var pid=arguments[1];   //父主题ID 
            var cid=arguments[2];   //子主题ID 
             $.ajax(
                    {
                        type:'post',
                        url:"/admin.php/Home/ActiviProduct/searchItem",
                        data:{'page':page,'pid':pid,'cid':cid},
                        dataType:"json",
                        success:function(message)
                        {
                            if(message.status)
                            {
                                var $page=message.pagearr; //底部分页
                                var $items=message.items;//商品
                                var $tbody=$('<tbody></tbody>');
                                var $string='';
                                $.each($items,function(key,item){
                                    $string+='<tr><td><input type="checkbox" value='+item['id']+'></td>';
                                    $string+='<td>'+item['activ_price']+'</td>';
                                    $string+='<td>'+item['cname']+'</td>';
                                    $string+='<td>'+item['pname']+'</td>';
                                    $string+='<td>'+item['itemprop']+'</td>';
                                    $string+='<td>'+item['limit_num']+'</td>';
                                    $string+='<td>'+item['activ_num']+'</td>';
                                    $string+='<td>'+item['res_inventory']+'</td>';
                                    $string+='<td><div class="am-btn-group am-btn-group-xs"><span class="am-icon-pencil-square-o"><a href="javascript:void(0)" class="editPro" value='+item['id']+'>修改</a></span>&nbsp;<span class="am-icon-trash"><a href="javascript:void(0)" class="delPro" value='+item['id']+'>删除</a></span></div></td>';
                                    $string+='</tr>';
                                }
                                )
                                
                                  $tbody.append($string);
                                  $($tbody).replaceAll(".activetalbe tbody");
                                  //替换分页
                                  $("#count").html($page.count);
                                  $("#currentpage").html($page.page);
                                  $("#pagesize").val($page.pagesize);
                                  $('#prepage').attr('value',$page.prevpage);
                                  $('#nexpage').attr('value',$page.nextpage);
                                  $('#laspage').attr('value',$page.pagetotal);
                                  $('#pagetotal').html($page.pagetotal);

                            }else{
                                    $('.activetalbe tbody').html('暂无数据');
                                 }
                            //
                        }
                      }
                );
        


        },
        //通过父子主题来筛选活动产品
        filterItem:function(){
            var pid=$("select[name='pthemeid'] option:selected").val();
            var cid=$("select[name='cthemeid'] option:selected").val();
            this.searchitem(1,pid,cid);
        },
        // 编辑活动产品
        editProduct:function(id){
            var aid=(id!=false)?id:0; //活动产品Id
             $.ajax(
                    {
                        type:'post',
                        url:"/admin.php/Home/ActiviProduct/ajaxEditProduct",
                        data:{'id':aid},
                        dataType:"json",
                        success:function(message)
                        {
                            if (message.status)
                            {
                                var $items=message.items;//商品
                                var $tbody=$('<tbody></tbody>');
                                var $string='';
                                $.each($items,function(key,item){
                                    $string+='<tr><td>产品名称:</td><td>'+item.itemprop+'</td></tr>';
                                    $string+='<tr><td>活动价格:</td><td> <input name="activ_price" value= '+item.activ_price+'></td></tr>';
                                    $string+='<tr><td>产品数量:</td><td> <input name="activ_num" value='+item.activ_num+'></td></tr>';
                                    $string+='<tr><td>限购数量:</td><td> <input name="limit_num" value='+item.limit_num+'></td></tr>';
                                    $string+='<tr><td>父主题:</td><td>'+item.pname+'</td></tr>';
                                    $string+='<tr><td>子主题:</td><td>'+item.cname+'</td></tr>';
                                })

                                $tbody.append($string);
                                $($tbody).replaceAll(".editProtable tbody");
                                //为弹出层设置活动产品ID
                                $("input[name='activitemid']").val(aid);

                                return false;
                            }

                            $('.editProtable tbody').html('暂无数据');
                            
                        }
                    }
                );
        },
        //保存活动产品
        saveActivItem:function(){
            var $activ_price=$("input[name='activ_price']").val();
            var $activ_num=$("input[name='activ_num']").val();
            var $limit_num=$("input[name='limit_num']").val();
            var $pid=$("select[name='ptheme']  option:selected").val();
            var $cid=$("select[name='ctheme']  option:selected").val();
            var $id=$("input[name='activitemid']").val(); //活动产品ID
             $.ajax(
                {
                    type:'post',
                    url:"/admin.php/Home/ActiviProduct/saveActivItem",
                    data:{'id':$id,'activ_price':$activ_price,'activ_num':$activ_num,'limit_num':$limit_num,'pid':$pid,'cid':$cid},
                    dataType:"json",
                    success:function(message)
                    {
                        if(message.status !=false)
                        {
                            alert(message.msg);
                            window.location.reload();
                            return false;
                          
                        }else{
                            alert(message.msg);
                            window.location.reload();
                        }

                    }
                  }
            );
        },
        //父主题，子主题两级联动
        twolinkage:function(pid){
            var $pid=(pid!=false)?pid:0;
             $.ajax(
                {
                    type:'post',
                    url:"/admin.php/Home/ActiviProduct/twolinkage",
                    data:{'pid':$pid},
                    dataType:"json",
                    success:function(message)
                    {
                        var $cthemes=message.cthemes;

                        if(message.status!=false)
                        {
                            $string='';
                            $.each($cthemes,function(key,ctheme){
                                $string+='<option value='+ctheme.id+' >'+ctheme.childtheme+'</option>';
                            })

                            $($string).replaceAll('.ctheme option');
                          
                        }
                    }
                  }
            );
        },
        //删除活动产品
        delActivItem:function(){
            var id=(arguments[0]!=false)?arguments[0]:0;
            $.ajax(
                 {
                    type:'post',
                    url:"/admin.php/Home/ActiviProduct/delActivItem",
                    data:{'id':id},
                    dataType:"json",
                    success:function(message)
                    {
                        if (message.status){
                            alert(message.msg);
                            window.location.reload();

                        }else{
                            alert(message.msg);
                            window.location.reload();
                        }
                    }
                  }
            )
        },
        //初始化弹出层中中的父主题
        getPtheme:function(){
            var pid=0;
            $.ajax(
                     {
                        type:'post',
                        url:"/admin.php/Home/ActiviProduct/getPtheme",
                        data:{'pid':pid},
                        dataType:"json",
                        success:function(message)
                        {
                            if (message.status)
                            {
                                var $pthemes=message.pthemes;
                                var $string='<option value=0> 请选择父主题</option>';
                                $.each($pthemes,function(key,ptheme){
                                    $string+='<option value='+ptheme.id+'>'+ptheme.parenttheme+'</option>';
                                })
                                $('.listptheme option').remove();
                                $('.listptheme').append($string);

                            };
                            return false;
                        }
                }
                )
        },
        

    }
    //绑定筛选事件
    $(document).on('click','.seachactiv',function(){
        $.Activi_Product.filterItem();
    })
    //重置父子，主题
    $(document).on('click','.resetsearch',function(){
        window.location.reload();

    })
    // 绑定翻页事件
    $(document).on('click','#firpage,#prepage,#nexpage,#laspage',function(){
            //获取父主题ID和子主题ID
            var pid=$(".ptheme").val();
            var cid=$(".ctheme").val();

            var pagevalue=$(this).attr('value'); //页数

            $.Activi_Product.searchitem(pagevalue,pid,cid);
    })
    //绑定编辑活动产品
    $(document).on('click','.editPro',function(){
        var id=$(this).attr('value');
        $.Activi_Product.editProduct(id);
         //弹出层
        $('#editPro').modal();
    })
    //绑定保存活动产品
    $(document).on('click','.saveactiv',function(){

        $.Activi_Product.saveActivItem();
    })
    //绑定取消按钮（关闭弹出框)
    $(document).on('click','.coloseactiv',function(){
        $('#editPro').modal('close');
        return false;
    })

    //当父主题变化时，子主题跟随变化
    $(document).on('change','.ptheme',function(){
        var pid=$("select[name='ptheme'] option:selected").val();
        $.Activi_Product.twolinkage(pid);
        return false;
    })
    //绑定删除活动产品事件
    $(document).on('click','.delPro',function(){
        var result=confirm('确定删除活动产品?');
        if(!result){return false;}
        var id=$(this).attr('value');
        $.Activi_Product.delActivItem(id);
    })
    //活动产品列表页面父子主题两级联动
    $(document).on('click','.listptheme',function(){
        var pid=$("select[name='pthemeid'] option:selected").val();
        $.Activi_Product.twolinkage(pid);
    })
    //ajax 加载活动产品列表父主题
    $(function(){
        $.Activi_Product.getPtheme();
    })
    
    
})(jQuery)
//添加产品模块
;(function($){
    $.addActivItem={
        //搜索所有产品
        searchAllItem:function(){
            var keywords=(arguments[0]!=false)?arguments[0]:'';
            var page=(arguments[1]!=false)?arguments[1]:1;
            $.ajax(
                    {
                        type:'post',
                        url:"/admin.php/Home/ActiviProduct/searchAllItem",
                        data:{'keywords':keywords,'page':page},
                        dataType:"json",
                        success:function(message)
                        {
                              if(message.status)
                              { 
                                var $items=message.items;
                                var $page=message.pagearr;
                                $tbody=$('<tbody></tbody>');
                                
                                $string='';
                                $.each($items,function(key,item){
                                    $string+='<tr id=pro_'+item.productid+' class='+item.itemid+'><td class="table-check"><input type="checkbox" class="checkboxitem" value='+item.productid+'></td>';
                                    $string+='<td>'+item.productid+'</td>';
                                    $string+='<td>'+item.itemname+item.bn+item.color+'</td>';
                                    $string+='<td>'+item.typename+'</td>';
                                    $string+='<td>总公司</td></tr>';
                                    
                                });
                                $tbody.append($string);
                                $($tbody).replaceAll(".addProtable tbody");
                                //替换分页
                                $(".count").html($page.count);
                                $(".addcurrentpage").html($page.page);
                                $("#pagesize").val($page.pagesize);
                                $('.resprepage').attr('value',$page.prevpage);
                                $('.resnexpage').attr('value',$page.nextpage);
                                $('.reslaspage').attr('value',$page.pagetotal);
                                $('.pagetotal').html($page.pagetotal);

                              }else{

                              } 
                        }
                     }
                    );
            

        },
        //初始化弹出层中中的父主题
        getPtheme:function(){
            var pid=0;
            $.ajax(
                     {
                        type:'post',
                        url:"/admin.php/Home/ActiviProduct/getPtheme",
                        data:{'pid':pid},
                        dataType:"json",
                        success:function(message)
                        {
                            if (message.status)
                            {
                                var $pthemes=message.pthemes;
                                var $string='';
                                $.each($pthemes,function(key,ptheme){
                                    $string+='<option value='+ptheme.id+'>'+ptheme.parenttheme+'</option>';
                                })
                                $('.addptheme option').remove();
                                $('.addptheme').append($string);

                            };
                            return false;
                        }
                }
                )
        },
        //选择添加项
        addItem:function(){
            var productid=arguments[0];
            var itemname=$('#pro_'+productid).children('td').eq(2).html();
            var typename=$('#pro_'+productid).children('td').eq(3).html();
            var itemid=$('#pro_'+productid).attr('class');
            $string='';
            $string+='<tr id=new_'+productid+'><td>'+productid+'</td>';
            $string+='<td>'+itemname+'</td>';
            $string+='<td>'+typename+'</td>';
            $string+='<td class="activtd"><input name="activ_price" type="text"></td>';
            $string+='<td class="activtd"><input name="activ_num" type="text"></td>';
            $string+='<td class="activtd"><input name="limit_num" type="text"></td>';
            $string+='<td class="activtd"><input name="itemid" type="hidden" value='+itemid+'></td>';
            $string+='<td ><a href="javascript:void(0)"> <span class="am-icon-trash-o  delItemRow" value='+productid+'></span></a></td>';
            $string+='</tr>';
            $('.searchresult tbody').append($string);
            
        },
        //保存添加项
        saveActivItem:function(){
            var productids=[];
            var activ_price=[];
            var limit_num=[];
            var activ_num=[];
            var itemid=[];
            var pid=$("select[name='addptheme'] option:selected").val();
            var cid=$("select[name='addctheme'] option:selected").val();
            if (pid ==false || cid == false){
                alert('请先选择父主题，子主题');
                return false;
            };
             $('.searchresult tbody  tr').each(function(){
                    productids.push($(this).find("td").eq(0).html());
                    activ_price.push($(this).find("input[name='activ_price']").val());              
                    limit_num.push($(this).find("input[name='limit_num']").val());
                    activ_num.push($(this).find("input[name='activ_num']").val());
                    itemid.push($(this).find("input[name='itemid']").val());
                });
            
            $.ajax(
                    {
                        type:'post',
                        url:"/admin.php/Home/ActiviProduct/saveAddItem",
                        data:{'productids':productids,'activ_price':activ_price,'limit_num':limit_num,'pid':pid,'cid':cid,'activ_num':activ_num,'itemid':itemid},
                        dataType:"json",
                        success:function(message)
                        {
                            if (message.status){
                                $('#addPro').modal('close');
                                window.location.reload();
                            }else{
                                alert(message.msg);
                                $('#addPro').modal('close');
                                window.location.reload();
                            }
                        }
                      }
                )

        },
    }
    //绑定搜索产品事件
    $(document).on('click','#activseach',function(){
        //获取关键词
        var keywords=$("input[name='keywords']").val();

        $.addActivItem.searchAllItem(keywords);
    })
    //绑定分页事件
    $(document).on('click','.resfirpage,.resprepage,.resnexpage,.reslaspage',function(){
        var pagevalue=$(this).attr('value'); //页数
        var keywords=$("input[name='keywords']").val();
        $.addActivItem.searchAllItem(keywords,pagevalue);
    })
    //弹出新增活动产品
    $(document).on('click','.addactiv',function(){
        //初始化弹出层中的父主题
        $.addActivItem.getPtheme();
        $('#addPro').modal();
        return false;
    })
    //取消弹出新增活动产品窗口
    $(document).on('click','.coloseActivItem',function(){
        $('#addPro').modal('close');
        return false;
    })
    //复制搜索结果到底部添加项
    $(document).on('click','.checkboxitem',function(){
        var produtid=$(this).val();
         $.addActivItem.addItem(produtid);
        
    })
    //保存添加项
    $(document).on('click','.addActivItem',function(){
        $.addActivItem.saveActivItem();
    })
    //删除选择的添加项
    $(document).on('click','.delItemRow',function(){
        var temp=confirm('确定删除？');
        if(!temp){ return false;}
        var produtid=$(this).attr('value');
        $('#new_'+produtid).remove();
        
    })
    //弹出层中父子主题两级联动
    $(document).on('click','.addptheme',function(){

        var pid=$("select[name='addptheme'] option:selected").val();
        $.Activi_Product.twolinkage(pid);
    })

})(jQuery)